<?php

namespace App\Http\Controllers\Api;

use App\City;
use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CityController extends Controller
{
    use ApiResponse;
    public function index()
    {
        //[{"label":"北京市","value"}]
       $province=City::with(['city'=>function($query){
           $query->with('area');
       }])->where('pid',0)->where('value',">",0)->get()->toArray();
       $data1=[];
       $data2=[];
       $data3=[];
       foreach ($province as $key=>$value){
           $p=[
               'value'=>$value['value'],
               'label'=>$value['label']
           ];
           $data1[$key]=$p;
           foreach ($value['city'] as $k=>$v){
               $p=[
                   'value'=>$v['value'],
                   'label'=>$v['label']
               ];
               $data2[$key][$k]=$p;
               if ($v['area']){
                   foreach ($v['area'] as $kk=>$vv){
                       $p=[
                           'value'=>$vv['value'],
                           'label'=>$vv['label']
                       ];
                       $data3[$key][$k][$kk]=$p;
                   }
               }else{
                   $data3[$key][$k]=[];
               }

           }

       }
       $result=[
           'province'=>$data1,
           'city'=>$data2,
           'area'=>array_values($data3)
       ];
       return $this->data($result);
    }

}
